import { createSlice, PayloadAction } from '@reduxjs/toolkit'

interface UserType {
	nickname: string,
	avator: string,
	id: -1,
	username: string
}

const initialState: UserType = {
	nickname: "",
	avator: '',
	id: -1,
	username: ''
}


const userSlice = createSlice({
	name: 'user',
	initialState,
	reducers: {
		loginSuccess(state, action: PayloadAction<UserType>) {
			state.nickname = action.payload.nickname
			state.avator = action.payload.avator
			state.id = action.payload.id
			state.username = action.payload.username
		},
		loginFail(state) {
			state.nickname = ''
			state.avator = ''
			state.id = -1
			state.username = ''
		}
	}
})
export const { loginSuccess, loginFail } = userSlice.actions

export default userSlice